import axios from 'axios'

/**
 * 获取工单列表
 * @param {Object} params 查询参数
 */
export const getWorkOrderList = (params) => {
  console.log('调用获取工单列表API, 参数:', params)
  // 确保参数中至少包含pageNum和pageSize
  if (!params.pageNum) params.pageNum = 1
  if (!params.pageSize) params.pageSize = 10
  
  return axios.post('/workOrder/list', params)
    .then(response => {
      console.log('获取工单列表API响应:', response)
      return response
    })
    .catch(error => {
      console.error('获取工单列表API错误:', error.response || error.message || error)
      throw error
    })
}

/**
 * 获取工单详情
 * @param {number} id 工单ID
 */
export const getWorkOrderDetail = (id) => {
  return axios.get(`/workOrder/${id}`)
}

/**
 * 创建工单
 * @param {Object} data 工单数据
 */
export const createWorkOrder = (data) => {
  return axios.post('/workOrder/create', data)
}

/**
 * 更新工单
 * @param {Object} data 工单数据
 */
export const updateWorkOrder = (data) => {
  return axios.put('/workOrder/update', data)
}

/**
 * 删除工单
 * @param {number} id 工单ID
 */
export const deleteWorkOrder = (id) => {
  return axios.delete(`/workOrder/${id}`)
}

/**
 * 更新工单状态
 * @param {Object} params 参数
 */
export const updateWorkOrderStatus = (params) => {
  return axios.post('/workOrder/status', null, { params })
}

/**
 * 获取工单类型列表
 */
export const getWorkOrderTypes = () => {
  console.log('调用获取工单类型API')
  return axios.get('/workOrder/types')
    .then(response => {
      console.log('获取工单类型API响应:', response)
      return response
    })
    .catch(error => {
      console.error('获取工单类型API错误:', error.response || error.message || error)
      throw error
    })
}

/**
 * 获取工单状态列表
 */
export const getWorkOrderStatuses = () => {
  console.log('调用获取工单状态API')
  return axios.get('/workOrder/statuses')
    .then(response => {
      console.log('获取工单状态API响应:', response)
      return response
    })
    .catch(error => {
      console.error('获取工单状态API错误:', error.response || error.message || error)
      throw error
    })
} 

/**
 * 获取工单处理记录列表
 * @param {number} orderId 工单ID
 */
export const getWorkOrderRecords = (orderId) => {
  return axios.get(`/workOrder/records/${orderId}`)
}

/**
 * 添加工单处理记录
 * @param {Object} data 处理记录数据
 */
export const addWorkOrderRecord = (data) => {
  return axios.post('/workOrder/record', data)
}

/**
 * 获取工单附件列表
 * @param {number} orderId 工单ID
 */
export const getWorkOrderAttachments = (orderId) => {
  return axios.get(`/workOrder/attachments/${orderId}`)
}

/**
 * 上传工单附件
 * @param {FormData} formData 包含文件和元数据的FormData对象
 */
export const uploadWorkOrderAttachment = (formData) => {
  return axios.post('/workOrder/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 删除工单附件
 * @param {number} attachmentId 附件ID
 */
export const deleteWorkOrderAttachment = (attachmentId) => {
  return axios.delete(`/workOrder/attachment/${attachmentId}`)
}

/**
 * 获取附件下载链接
 * @param {number} attachmentId 附件ID
 */
export const getAttachmentDownloadUrl = (attachmentId) => {
  return `/workOrder/download/${attachmentId}`
} 
 